#coding=utf-8
import datetime

from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
import django.contrib.auth as auth  
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required

from django.template import RequestContext

from forum.models import *

def list_groups(request):
    return render_to_response('forum/list_groups.html',
            {'categories':Category.objects.all()},
            context_instance=RequestContext(request)
            )
    
def list_threads(request, group):
    group_ = Group.objects.get(slug=group)
    threads = Thread.objects.filter(group__slug=group).order_by("created_at").all()
    
    i = 1
    for t in threads:
        t.no = i
        i += 1
    
    return  render_to_response("forum/list_threads.html", 
                {"threads":threads, "group":group_}, 
                context_instance=RequestContext(request))  
    
@login_required(redirect_field_name='previous')
def add_thread(request, group):
    if request.method == 'GET':
        return render_to_response('forum/add_thread.html',
                                  {'group':group},
                                  context_instance=RequestContext(request))
    else:
        title = request.POST['title']
        content = request.POST['content']
        
        group_ = Group.objects.get(slug=group)
        
        user = request.user
        
        t = Thread()
        t.title = title
        t.group = group_
        t.author = user
        t.last_active = datetime.datetime.now()
        t.save()
        
        p = Post()
        p.content = content
        p.thread = t
        p.author = user
        
        p.save()
        
        '''
        return HttpResponse("<h3>%s</h3>by<b>%s</b><p>%s</p><p>%s</p>"%
                           (title, user.username, group.description, content))
        '''
        return HttpResponseRedirect('/%s/list_threads'%group)
        
@login_required(redirect_field_name='previous')        
def append_thread(request, thread_id):
    thread = Thread.objects.get(id=thread_id)
    if request.method == 'GET':
        return render_to_response('forum/append_thread.html',
                                  {'thread':thread},
                                  context_instance=RequestContext(request))
    else:
        content = request.POST['content']
        
        p = Post()
        p.content = content
        p.thread = thread
        p.author = request.user
        
        p.save()
        
        thread.post_count += 1
        thread.last_active = datetime.datetime.now()
        
        thread.save()
        
        return HttpResponseRedirect('/details_thread/%s'%thread.id)
        
def details_thread(request, thread_id):
    thread = Thread.objects.get(id=thread_id)
    
    thread.hits += 1
    thread.save()
    return render_to_response("forum/details_thread.html", 
                    {"thread":thread},
                     context_instance=RequestContext(request)
                    )  

